﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Threading;

namespace NewsWorm {
    internal class FirstTimeInstall {
        internal FirstTimeInstall( RegistryManager manager ) {
            m_Manager = manager;
        }

        private RegistryManager m_Manager;

        internal void MarkAsInstalled() {
            if ( m_Manager.IsFirstRun ) {
                new Thread( MarkAsInstalledThread ).Start();
            }
        }

        internal void MarkAsInstalledThread() {
            try {
                SHA256 sha = SHA256Managed.Create();
                long d = DateTime.Now.ToBinary();
                string guid = BitConverter.ToString( sha.ComputeHash( BitConverter.GetBytes( d ) ) );
                string url = "http://www.newsworm.net/install/?guid=" + guid;

                string responseText;

                HttpWebRequest httpWebRequest =
                   (HttpWebRequest)WebRequest.Create( url );
                httpWebRequest.Timeout = 5000;
                httpWebRequest.ContentType = "text/html";
                httpWebRequest.Method = "GET";

                HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using ( var streamReader = new StreamReader( httpResponse.GetResponseStream() ) ) {
                    responseText = streamReader.ReadToEnd();
                }
                Console.WriteLine( responseText );
            } catch ( Exception ex ) {
                Console.WriteLine( ex.Message );
            }
        }
    }
}
